Spring AOP 切面@Around注解的具体使用

您所在的位置:网站首页 pointcut annotation Spring AOP 切面@Around注解的具体使用

Spring AOP 切面@Around注解的具体使用

#Spring AOP 切面@Around注解的具体使用| 来源: 网络整理| 查看: 265

大家好,又见面了,我是你们的朋友全栈君。

@Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务。

比如我们想在执行controller中方法前打印出请求参数,并在方法执行结束后来打印出响应值,这个时候,我们就可以借助于@Around注解来实现;

再比如我们想在执行方法时动态修改参数值等

类似功能的注解还有@Before等等,用到了Spring AOP切面思想,Spring AOP常用于拦截器、事务、日志、权限验证等方面。

完整演示代码如下:

需要说明的是,在以下例子中,我们即可以只用@Around注解,并设置条件,见方法run1();也可以用@Pointcut和@Around联合注解,见方法pointCut2()和run2(),这2种用法是等价的。如果我们还想利用其进行参数的修改,则调用时必须用joinPoint.proceed(Object[] args)方法,将修改后的参数进行回传。如果用joinPoint.proceed()方法,则修改后的参数并不会真正被使用。

import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.persistence.EntityManager; /** * 控制器切面 * * @author lichuang */ @Component @Aspect public class ControllerAspect { private static final Logger logger = LoggerFactory.getLogger(ControllerAspect.class); @Autowired private EntityManager entityManager; /** * 调用controller包下的任意类的任意方法时均会调用此方法 */ @Around("execution(* com.company.controller.*.*(..))") public Object run1(ProceedingJoinPoint joinPoint) throws Throwable { //获取方法参数值数组 Object[] args = joinPoint.getArgs(); //得到其方法签名 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); //获取方法参数类型数组 Class[] paramTypeArray = methodSignature.getParameterTypes(); if (EntityManager.class.isAssignableFrom(paramTypeArray[paramTypeArray.length - 1])) { //如果方法的参数列表最后一个参数是entityManager类型,则给其赋值 args[args.length - 1] = entityManager; } logger.info("请求参数为{}",args); //动态修改其参数 //注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args) Object result = joinPoint.proceed(args); logger.info("响应结果为{}",result); //如果这里不返回result,则目标对象实际返回值会被置为null return result; } @Pointcut("execution(* com.company.controller.*.*(..))") public void pointCut2() {} @Around("pointCut2()") public Object run2(ProceedingJoinPoint joinPoint) throws Throwable { //获取方法参数值数组 Object[] args = joinPoint.getArgs(); //得到其方法签名 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); //获取方法参数类型数组 Class[] paramTypeArray = methodSignature.getParameterTypes(); if (EntityManager.class.isAssignableFrom(paramTypeArray[paramTypeArray.length - 1])) { //如果方法的参数列表最后一个参数是entityManager类型,则给其赋值 args[args.length - 1] = entityManager; } logger.info("请求参数为{}",args); //动态修改其参数 //注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args) Object result = joinPoint.proceed(args); logger.info("响应结果为{}",result); //如果这里不返回result,则目标对象实际返回值会被置为null return result; } }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159754.html原文链接:https://javaforall.cn



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3